package Mail;

import java.security.Security;
import java.util.Date;
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;

import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPStore;

public class ImapServer {
	public static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
	
	String host;
	int port;
	String usr;
	String pwd;
	public ImapServer(String host,int port,String usr,String pwd){
		this.host = host;
		this.port = port;
		this.usr = usr;
		this.pwd = pwd;
	}
	
	public IMAPStore Connect() {
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
		/* Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        这里有一个错我是这么解决的（Windows -> Preferences，Java/Compiler/Errors/Warnings->
		Deprecated and restricted API， Forbidden reference (access rules)，原始设定为Error修改为Warning）*/
		Properties props = System.getProperties();  
		props.setProperty("mail.imap.socketFactory.class", SSL_FACTORY);  
		props.setProperty("mail.imap.socketFactory.port",Integer.toString(port));  
		props.setProperty("mail.store.protocol","imap");    
		props.setProperty("mail.imap.host", host);    
		props.setProperty("mail.imap.port", Integer.toString(port));    
		props.setProperty("mail.imap.auth.login.disable", "true");
		Session session = Session.getDefaultInstance(props,null);  
		session.setDebug(false);
		
		IMAPStore store = null;
		try {
			store = (IMAPStore)session.getStore("imap");	// 使用imap会话机制，连接服务器
			store.connect(host,usr,pwd);	
		} catch (NoSuchProviderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return store;
	}
	
	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getUsr() {
		return usr;
	}

	public void setUsr(String usr) {
		this.usr = usr;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

}
